/**
 * js
 */
angular.module('mall.shopcart.base', [
        "ngStorage"
    ])

    .config(['$stateProvider', '$urlRouterProvider',
        function ($stateProvider, $urlRouterProvider) {
            $stateProvider
                .state('mall.shopcart', {
                    url: '/shopcart',
                    'abstract': true,
                    template: '<ui-view/>',
                    controller: 'ShopCartBaseController'
                })
            ;
        }])

    //购物车控制器
    .controller('ShopCartBaseController', ['$scope', '$state', 'ShopcartService',
        function ($scope, $state, ShopcartService) {
            $scope.mall_enturl = projectBaseUrl;
            $scope.all_goods_list = ShopcartService.getAllShopCarInfo();
            $scope.all_goods_list_price = ShopcartService.getallshoppriceandcount();
            //添加商品
            $scope.addgoods = function (shopid, cid, data) {
                ShopcartService.addgoods(shopid, cid, data.goodsid);
                for (var i = 0; i < $scope.all_goods_list.length; i++) {
                    var goodsitem = $scope.all_goods_list[i];
                    if (goodsitem.shopid == shopid)
                        break;
                }
                for (var k = 0; k < goodsitem.list.length; k++) {
                    for (var j = 0; j < goodsitem.list[k].goods.length; j++) {
                        if (goodsitem.list[k].goods[j].goodsid == data.goodsid) {
                            var buycount = goodsitem.list[k].goods[j].buycount;
                            if (buycount > 999) {
                                input_alert('超出购买范围');
                                goodsitem.list[k].goods[j].buycount = 999;
                            } else {
                                goodsitem.goodscontent.totalprice += goodsitem.list[k].goods[j].price;
                                goodsitem.goodscontent.count++;
                                goodsitem.goodscontent.integration = goodsitem.goodscontent.totalprice / 100;
                            }
                        }
                    }
                }
                $scope.all_goods_list_price = ShopcartService.getallshoppriceandcount();
            }
            //移出商品
            $scope.removegoods = function (shopid, cid, data) {
                for (var i = 0; i < $scope.all_goods_list.length; i++) {
                    var goodsitem = $scope.all_goods_list[i];
                    if (goodsitem.shopid == shopid)
                        break;
                }
                for (var k = 0; k < goodsitem.list.length; k++) {
                    for (var j = 0; j < goodsitem.list[k].goods.length; j++) {
                        if (goodsitem.list[k].goods[j].goodsid == data.goodsid) {
                            goodsitem.goodscontent.totalprice -= goodsitem.list[k].goods[j].price;
                            goodsitem.goodscontent.count--;
                            goodsitem.goodscontent.integration = goodsitem.goodscontent.totalprice / 100;
                        }
                    }
                }
                ShopcartService.removeGoods(shopid, cid, data.goodsid);
                $scope.all_goods_list_price = ShopcartService.getallshoppriceandcount();
            }

            //完全移出商品
            $scope.myque_remove = function (shopid, cid, data) {
                $(".contain-item").fadeIn();
                $scope.clean = function () {
                    for (var i = 0; i < $scope.all_goods_list.length; i++) {
                        var goodsitem = $scope.all_goods_list[i];
                        if (goodsitem.shopid == shopid)
                            break;
                    }
                    for (var k = 0; k < goodsitem.list.length; k++) {
                        for (var j = 0; j < goodsitem.list[k].goods.length; j++) {
                            if (goodsitem.list[k].goods[j].goodsid == data.goodsid) {
                                goodsitem.goodscontent.count -= goodsitem.list[k].goods[j].buycount;
                                goodsitem.goodscontent.totalprice -= goodsitem.list[k].goods[j].price * goodsitem.list[k].goods[j].buycount;
                                goodsitem.goodscontent.integration = goodsitem.goodscontent.totalprice / 100;
                                goodsitem.list[k].goods.splice(j, 1);
                            }
                        }
                    }
                    ShopcartService.cleanById(shopid, cid, data.goodsid);
                    $scope.all_goods_list_price = ShopcartService.getallshoppriceandcount();
                    $(".contain-item").fadeOut();
                }
            }

            /*$scope.buy_change = function (shopid, data) {
                for (var i = 0; i < $scope.all_goods_list.length; i++) {
                    var goodsitem = $scope.all_goods_list[i];
                    if (goodsitem.shopid == shopid)
                        break;
                }
                for (var k = 0; k < goodsitem.list.length; k++) {
                    for (var j = 0; j < goodsitem.list[k].goods.length; j++) {
                        if (goodsitem.list[k].goods[j].goodsid == data.goodsid) {
                            var buycount = goodsitem.list[k].goods[j].buycount
                            if (buycount < 1 && buycount != null) {
                                input_alert('超出购买范围')
                                goodsitem.list[k].goods[j].buycount = 1
                            } else if (buycount > 999) {
                                input_alert('超出购买范围')
                                goodsitem.list[k].goods[j].buycount = 999
                            }
                            if (buycount != null) {
                                ShopcartService.importGoods(shopid, data.cataid, data.goodsid);
                                $scope.goodscontent = ShopcartService.gettotalpriceandcount(shopid);
                                goodsitem.goodscontent.totalprice = $scope.goodscontent.totalprice;
                                goodsitem.goodscontent.count = $scope.goodscontent.count;
                                goodsitem.goodscontent.integration = $scope.goodscontent.integration;
                            }
                        }
                    }
                }
            }*/
            $scope.input = function (shopid, cid,data) {
                $scope.goodscontent = ShopcartService.gettotalpriceandcount(shopid);
                for (var i = 0; i < $scope.all_goods_list.length; i++) {
                    var goodsitem = $scope.all_goods_list[i];
                    if (goodsitem.shopid == shopid)
                        break;
                }
                for (var k = 0; k < goodsitem.list.length; k++) {
                    for (var j = 0; j < goodsitem.list[k].goods.length; j++) {
                        if (goodsitem.list[k].goods[j].goodsid == data.goodsid) {
                            var buycount = goodsitem.list[k].goods[j].buycount
                            if (buycount == null) {
                                input_alert('输入有误')
                                goodsitem.list[k].goods[j].buycount = 1;
                                data.buycount = 1;
                                $scope.all_goods_list = ShopcartService.getAllShopCarInfo();
                            }
                            ShopcartService.importGoods(shopid, cid, data.goodsid,data.buycount);
                        }
                    }
                }
                $scope.all_goods_list_price = ShopcartService.getallshoppriceandcount();
            }
            $scope.edit = function (shopid) {
                var editId = "edit" + shopid;
                var goodsEditId = "goods_edit" + shopid;
                var goodsCompleteId = "edit_complete" + shopid;
                var deleteId = "delete" + shopid;
                cartGoodsEdit(editId, goodsEditId, goodsCompleteId, deleteId);
            }
        }
    ])

;
